package controller

import (
	backend "AccessAdmin/api/backend"
	"AccessAdmin/internal/consts"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/model/entity"
	"AccessAdmin/internal/service"
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

// 用户管理
var User = cUser{}

type cUser struct{}

// 退出登录
func (c *cUser) Logout(ctx context.Context, req *backend.UserLogoutReq) (res *backend.UserLogoutRes, err error) {
	if err = service.User().Logout(ctx); err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.RedirectTo(consts.UserLoginUrl)
	return
}

// 渲染用户列表
func (c *cUser) ShowIndex(ctx context.Context, req *backend.UserIndexReq) (res *backend.UserIndexRes, err error) {
	service.View().Render(ctx, model.View{
		Title: "用户列表",
	})
	return
}

// 获取用户列表
func (c *cUser) IndexData(ctx context.Context, req *backend.UserIndexListReq) (res *backend.UserIndexListRes, err error) {
	res = &backend.UserIndexListRes{}
	_data, err := service.User().GetList(ctx, model.UserGetListInput{
		Page:     req.Page,
		Size:     req.Limit,
		Username: req.Username,
		Status:   req.Status,
	})
	if _data != nil && err == nil {
		res.Data = _data.List
		res.Count = _data.Total
	}
	return
}

func (c *cUser) ShowCreate(ctx context.Context, req *backend.UserShowCreateReq) (res *backend.UserShowCreateRes, err error) {
	service.View().Render(ctx, model.View{
		Title:   "用户创建",
		MainTpl: "admin/user/form.html",
		Data: g.Map{
			"action":    "/admin/user/create",
			"userModel": &entity.UserAdmin{},
		},
	})
	return
}

// 创建
func (c *cUser) Create(ctx context.Context, req *backend.UserCreateReq) (res *backend.UserCreateRes, err error) {
	out, err := service.User().Create(ctx, model.UserCreateInput{
		UserCreateUpdateBase: model.UserCreateUpdateBase{
			Username: req.Username,
			RealName: req.RealName,
			Password: req.Password,
			Phone:    req.Phone,
			Email:    req.Email,
			Status:   req.Status,
			Roles:    req.Roles,
		},
	})
	if err != nil {
		return nil, err
	}
	return &backend.UserCreateRes{UserId: out.UserId}, nil
}

func (c *cUser) ShowUpdate(ctx context.Context, req *backend.UserShowUpdateReq) (res *backend.UserShowUpdateRes, err error) {
	userModel, err := service.User().GetDetail(ctx, model.UserGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("用户信息错误!")
	}
	service.View().Render(ctx, model.View{
		Title:   "用户修改",
		MainTpl: "admin/user/form.html",
		Data: g.Map{
			"action":    "/admin/user/update/" + gconv.String(userModel.Id),
			"userModel": userModel,
		},
	})
	return
}

func (c *cUser) ShowView(ctx context.Context, req *backend.UserShowViewReq) (res *backend.UserShowViewRes, err error) {
	userModel, err := service.User().GetDetail(ctx, model.UserGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("用户信息错误!")
	}
	service.View().Render(ctx, model.View{
		Title:   "用户查看",
		MainTpl: "admin/user/view.html",
		Data: g.Map{
			"userModel": userModel,
		},
	})
	return
}

// 修改
func (c *cUser) Update(ctx context.Context, req *backend.UserUpdateReq) (res *backend.UserUpdateRes, err error) {
	err = service.User().Update(ctx, model.UserUpdateInput{
		Id: req.Id,
		UserCreateUpdateBase: model.UserCreateUpdateBase{
			Username: req.Username,
			RealName: req.RealName,
			Password: req.Password,
			Phone:    req.Phone,
			Email:    req.Email,
			Status:   req.Status,
			Roles:    req.Roles,
		},
	})
	return
}

// ShowUpdatePassword 渲染修改密码
func (c *cUser) ShowUpdatePassword(ctx context.Context, req *backend.UserShowUpdatePasswordReq) (res *backend.UserShowUpdatePasswordRes, err error) {
	service.View().Render(ctx, model.View{
		Title:   "修改密码",
		MainTpl: "admin/user/update-password.html",
	})
	return
}

// UpdatePassword 修改密码
func (c *cUser) UpdatePassword(ctx context.Context, req *backend.UserUpdatePasswordReq) (res *backend.UserUpdatePasswordRes, err error) {
	err = service.User().UpdatePassword(ctx, model.UserUpdatePasswordInput{
		OldPassword: req.OldPassword,
		NewPassword: req.NewPassword,
		RepPassword: req.RepPassword,
	})
	return
}

// ShowInfo 用户个人信息
func (c *cUser) ShowInfo(ctx context.Context, req *backend.UserShowInfoReq) (res *backend.UserShowInfoRes, err error) {
	userEntity := service.Session().GetUser(ctx)
	userModel, err := service.User().GetDetail(ctx, model.UserGetDetailInput{
		Id: userEntity.Id,
	})
	if err != nil {
		return nil, gerror.New("用户信息错误!")
	}
	service.View().Render(ctx, model.View{
		Title:   "用户中心",
		MainTpl: "admin/user/info.html",
		Data: g.Map{
			"userModel": userModel,
		},
	})
	return
}

// UpdateInfo 用户个人信息
func (c *cUser) UpdateInfo(ctx context.Context, req *backend.UserUpdateInfoReq) (res *backend.UserUpdateInfoRes, err error) {
	err = service.User().UpdateInfo(ctx, model.UserUpdateInfoInput{
		AvatarUrl:      req.AvatarUrl,
		IsUpdateAvatar: req.IsUpdateAvatar,
		Phone:          req.Phone,
		Email:          req.Email,
		RealName:       req.RealName,
		Remark:         req.Remark,
	})
	return
}
